package service

import (
	"manhe/dao"
	"manhe/model"
	"math/rand"
)

type BindBoxService struct {
}

//创建盲盒

func (*BindBoxService) Addbindbox(box model.BindBox) bool {

	d := dao.BlindBoxDao{}
	res := d.AddBlindBox(box)
	if res {
		return true
	}
	return false
}

// 修改盲盒信息

// 待完善 盲盒批次&商品的对应关系检测
func (*BindBoxService) Modifybindbox(box model.BindBox) bool {
	d := dao.BlindBoxDao{}
	res := d.Modifybindbox(box)
	if res {
		return true
	}
	return false
}

// 查询盲盒详情数据
func (*BindBoxService) Getbindbox(id int) *model.BindBox {
	d := dao.BlindBoxDao{}
	res, err := d.Getbindbox(id)
	if err != nil {
		return nil
	}

	return res
}

// 分页查询盲盒列表
func (*BindBoxService) Getbindboxlist(boxlist *model.Boxlist, box *model.BindBox) *model.Pagelist {
	var pagelist model.Pagelist

	//获取分页列表数据
	d := dao.BlindBoxDao{}
	list, err := d.Getbindboxlist(boxlist, box)

	if err != nil {
		return nil
	}

	//获取总条数
	count, err := d.CountBindBox(box)
	if err != nil {
		return nil
	}
	//组合查询结果
	pagelist.Datas = list
	pagelist.PageNum = boxlist.PageNum
	pagelist.PageSize = boxlist.PageSize
	pagelist.Total = count

	return &pagelist
}

// 随机获取盲盒批次奖励
func (*BindBoxService) GetRandomPc(id int) (*model.RandomPc, error) {
	//查询盲盒详情信息
	d := dao.BlindBoxDao{}
	box, err := d.Getbindbox(id)
	if err != nil {
		return nil, err
	}

	//随机选择一个批次
	num := len(box.BoxPcs)
	i := rand.Intn(num)
	pclist := box.BoxPcs[i]
	//组合
	var data model.RandomPc

	data.BoxId = box.ID
	data.BoxName = box.BoxName
	data.BatchNo = pclist.BatchNo
	data.BoxCover = box.BoxCover

	for _, v := range pclist.BatchPrizes {
		data.BatchPrizes = append(data.BatchPrizes, v.ID)
	}

	return &data, nil
}
